package com.android.tea;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.Application;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

public class TeaEncyclopediaApp extends Application
{	
	public static Context mContext;
	private static int screenWidth;
	private static int screenHeight;
	public static ArrayList<Activity> list =  new ArrayList<Activity>();
	
	public static int getScreenWidth(){
		return screenWidth;
	}
	
	public static int getScreenHeight(){
		return screenHeight;
	}
	
	@Override
	public void onCreate() 
	{
		super.onCreate();
		mContext = getApplicationContext();
		
		DisplayMetrics dm = new DisplayMetrics();
		WindowManager WM = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
		WM.getDefaultDisplay().getMetrics(dm);
		screenHeight=dm.heightPixels;
		screenWidth=dm.widthPixels;
		
	}
	
	public static boolean isAppOnForeground() {
		ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
		if (tasksInfo.size() > 0) {
			// 应用程序位于堆栈的顶层
			if ("com.android.tea".equals(tasksInfo.get(0).topActivity.getPackageName())) {
				return true;
			}
		}
		return false;
	}
	
	public static boolean isActivityOnForeground(Activity activity) {
		ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
		if (tasksInfo.size() > 0) {
			// activity位于堆栈的顶层
			if (activity.getClass().getName().equals(tasksInfo.get(0).topActivity.getClassName())) {
				return true;
			}
		}
		return false;
	}

	public static Context getAppContext() {
		return mContext;
	}
	
	public static void exit() {
		for(Activity activity : list) {
			activity.finish();	
		}
	}

}
